<template>
  <section class="question-list">
    <question-view :visible.sync="questionVisible" :qid="qid"></question-view>
    <question-edit :visible.sync="dialogVisible" @listPage="listPage" :sid="sid"></question-edit>
    <el-row>
      <el-col :span="4">
        <el-button type="primary" round @click="dialogVisible=true">新建题目</el-button>
      </el-col>
      <el-col :span="12">
        <el-input v-model="search" placeholder="请输入内容"></el-input>
      </el-col>
      <el-col :span="4" :offset="2">
        <el-button type="warning" round @click="doSearch()">搜索题干</el-button>
        <el-switch v-model="pic"></el-switch>只显示图片
      </el-col>
    </el-row>
    <el-table :data="tableData" stripe height="calc(100vh - 215px)" :style="{marginTop:'10px'}">
      <el-table-column prop="Id" label="ID" width="100">
      </el-table-column>
      <el-table-column prop="Stem" label="题干" width="600">
      </el-table-column>
      <el-table-column label="日期" width="240">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{scope.row.CreatedAt}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click.native.prevent="goQuestion(scope.row)"
                     type="text" size="small">
            详情<i class="el-icon-d-arrow-right"></i>
          </el-button>
          <el-button @click.native.prevent="del(scope.row)"
                     type="text" size="small">
            删除<i class="el-icon-d-arrow-right"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :style="{marginTop:'10px'}"
      @size-change="sizeChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 40, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      background
      @current-change="listPage"
      :total="total">
    </el-pagination>
  </section>
</template>

<script>
import {QuestionService} from '@/service/api'
import EventBus from '@/service/event'
import QuestionEdit from './Edit'
import QuestionView from './View'

export default {
  components: {
    QuestionEdit,
    QuestionView
  },
  data: function () {
    return {
      total: 0,
      tableData: [],
      qid: 0,
      pic: false,
      pageSize: 10,
      currentPage: 1,
      dialogVisible: false,
      questionVisible: false,
      search: ''
    }
  },
  computed: {
    sid: function () {
      return this.$route.query.sid
    }
  },
  mounted: function () {
    this.listPage()
    EventBus.$emit('acticePath', 'question')
  },
  methods: {
    sizeChange: function (val) {
      this.pageSize = val
      this.listPage()
    },
    listPage: function (val) {
      if (this.pic) {
        QuestionService.pic(val, this.pageSize)
          .then(res => {
            if (res) {
              this.tableData = res.questions
              this.total = res.total
            }
          })
      } else {
        QuestionService.page(val, this.pageSize, this.sid)
          .then(res => {
            if (res) {
              this.tableData = res.questions
              this.total = res.total
            }
          })
      }
    },
    goQuestion: function (item) {
      this.qid = item.Id
      this.questionVisible = true
    },
    del: function (item) {
      // todo delete
    },
    doSearch: function () {
      QuestionService.search(this.search)
        .then(res => {
          this.total = res.total
          this.tableData = res.questions
        })
    }
  }
}
</script>

<style scoped>
</style>
